package org.tea.inventory.db.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.tea.inventory.db.dao.AlumniVisitorMapper;
import org.tea.inventory.db.domain.AlumniVisitor;
import org.tea.inventory.db.util.VisitorUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AlumniVisitorService {
    @Resource
    private AlumniVisitorMapper alumniVisitorMapper;

    public PageInfo<AlumniVisitor> queryAlumniVisitor(AlumniVisitor alumniVisitor, Integer page, Integer limit, String sort, String order) {
        PageHelper.startPage(page, limit);
        List<AlumniVisitor> list = alumniVisitorMapper.selectAlumniVisitor(alumniVisitor);
        PageInfo<AlumniVisitor> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    public int saveAlumniVisitor(AlumniVisitor alumniVisitor) {
        int state = 0;
        List<AlumniVisitor> visitorList = alumniVisitorMapper.selectAlumniVisitor(alumniVisitor);
        if(visitorList.size() == 0)
            state = alumniVisitorMapper.insertSelective(alumniVisitor);
        return state;
    }

    public int uploadVisitor(AlumniVisitor visitor) {
        return alumniVisitorMapper.updateByPrimaryKeySelective(visitor);
    }

    public AlumniVisitor findAlumniVisitorByVisitorId(Integer visitorId) {
        return alumniVisitorMapper.selectByPrimaryKey(visitorId);
    }

    public void delete(Integer id) {
        AlumniVisitor visitor = new AlumniVisitor();
        visitor.setId(id);
        visitor.setStatus(VisitorUtil.STARTE_DELETED);
        alumniVisitorMapper.updateByPrimaryKeySelective(visitor);
    }

    public AlumniVisitor findById(Integer id) {
        return alumniVisitorMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据访客id查询访客记录
     * @param alumniVisitor
     * @return
     */
    public List<AlumniVisitor> findAlumniVisitor(AlumniVisitor alumniVisitor) {
        return alumniVisitorMapper.selectAlumniVisitor(alumniVisitor);
    }
}
